/**
 * Created by zlj on 2016/11/30.
 */
require('easyui')
var $alert = require("libs/alert")
var $confirm = require("libs/confirm")
var isSale = require('libs/user-type').isSale;
$(function () {
    tableinit();
    appendOption('api/v1/p/mms/internationalLogisticsProvider/queryAll',{}
        ,$("#code"));
    $('body').on('click',"a[name=delete]",function () {
        // $confirm("确认删除此条物流渠道",function () {
            var rowDatas = $('#logistics_channel').datagrid('getRows');
            var id = rowDatas[$(this).attr('row')].id;
            var url = 'api/v1/p/mms/internationalLogisticsProvider/delete';
            optFun(url,{id:id});
        // });
    });

    $("#logistics_search").on('click',function () {
        $('#logistics_channel').datagrid('load',{'code':$("#tb input[name=code]").val()});
    });

    $("body").on('change',"select[name=useSts]",function () {
         var rowDatas = $('#logistics_channel').datagrid('getRows');
         var row = $(this).parents("tr").find("a[name=delete]").attr("row");
         var id =  rowDatas[row].id;
         var url = 'api/v1/p/mms/internationalLogisticsProvider/operate';
         optFun(url,{id:id,useSts:$(this).val()});
    })
});

function tableinit() {
    $("#logistics_channel").datagrid({
        url:'api/v1/p/mms/internationalLogisticsProvider/list',
        border : false,
        height:$(window).height(),
        pagination : true,
        idField : 'id',
        pageSize : 20,
        pageList : [ 20, 50,  100 ],
        /*sortName : 'receiveDt',
        sortOrder : 'desc',*/
        checkOnSelect : false,//单击选中
        emptyMsg : "暂无数据",
        selectOnCheck : false,
        rownumbers:true,
        columns:[[
            {
                field : 'code',
                title:'国际物流渠道简称',
                width : '10%',
                formatter:function (value,row,index) {
                    return "<a href='Logistics-provider-detail?id="+row.id+"'>"+value+"</a>"
                }
            },
            {
            field : 'name',
            title : '国际物流渠道中文名称',
            width:'14%'
        },{
            field : 'linkman',
            title : '联系人',
            width:'8%',
        },{
            field:'linkTelphone',
            title:'联系电话',
            width:'8%',
        },{
            field:'address',
            title:'联系地址',
            width:'20%',
        },{
            field:'serviceCategory',
            title:'服务类别',
            width:'15%',
            formatter:function (value,row,index) {
                var res = '';
                if(row.hasAir=='Y'){
                   res +="空运,";
                }
                if(row.hasOcean=='Y'){
                    res +="海运,";
                }
                if(row.hasRailway=='Y'){
                    res +="国际铁路,";
                }
                if(row.hasExpress=='Y'){
                    res +="国际快递";
                }
                return res;
            }
        },{
            field:'memo',
            title:'备注',
            width:'5%',
        },{
            field:'useSts',
            title:'状态',
            width:'6%',
            formatter:function (value,row,index) {
               return "<select name='useSts'><option "+(value=='Y'?'selected':'')+" value='Y'>启用</option><option " +
                   (value=='N'?'selected':'')+" value='N'>停用</option></select>"
            }
        },{
            field:'opt',
            title:'操作',
            hidden:true,
            width:'10%',
            formatter:function (value,row,index) {
                if(row.useSts=='Y'){

                    return "<a href='Logistics-provider-update?id="+row.id+"'>修改</a>|<a href='javascript:;' name='delete' row='"+index+"'>删除</a>"
                }else{
                    return "<a href='javascript:;' name='delete' row='"+index+"'>删除</a>"
                }

            }
        }]],
        loadFilter:function(data){
            if(data.code!='200'){
                return data.data;
            }
            return data.data;
        },
        toolbar:"#tb",
    });
    $("#tbar").show();
}

// ajax 操作 url data-发送的数据
function optFun(url,data) {
    $.ajax({
        url:url,
        type:'post',
        data:data,
        dataType:'json',
        success:function (result) {
            if(result.code == 200){

            }else{
                $alert(result.message);
            }
            $('#logistics_channel').datagrid('reload');
        },
        error:function (e) {
            console.error("接口url:"+url+" 请求失败");
        }
    })
}

//添加参数
function appendOption(url,data,$select) {
    $select.combobox({
        url:url,
        valueField:'id',
        textField:'code',
        loadFilter:function(result){
            if(result.code=='200'){
                result.data.unshift({id:'',code:'全部'});
                return result.data;
            }
        }
    })
}
